<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/alllayui/layui/css/layui.css}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<script type="text/javascript">
    var btnCls = ['layui-btn-warm','layui-btn-danger','layui-btn-disabled','layui-btn-normal'];
	function dict(rowD,str){ 
		var ret = "";
		var btnIndex = 0;
		if(str=="nstate"){
			if(rowD.nstate==0){
				btnIndex = 0;
				ret = "起草";
			} 
			if(rowD.nstate==1){
				btnIndex = 1;
				ret = "发表";
			} 
		}
		 
		if(str=="newsType"){
			if(rowD.newsType==0){
				btnIndex = 0;
				ret = "内部使用";
			} 
			if(rowD.newsType==1){
				btnIndex = 1;
				ret = "外部使用";
			} 
		}
		if(str=="allowComment"){
			if(rowD.allowComment==0){
				btnIndex = 0;
				ret = "开启评论";
			} 
			if(rowD.allowComment==1){
				btnIndex = 1;
				ret = "关闭评论";
			} 
		}
		 
		var btn = "<a class=\"layui-btn layui-btn-xs layui-btn-radius "+ btnCls[btnIndex]+ "\">"+ ret +"</a>"; 
		return btn;
	}
</script>
<body>
 
<div class="layui-form searchTable">
    时间：
     <div class="layui-inline">  
      <div class="layui-input-inline">
        <input type="text" name="publishTimeStart" id="publishTimeStart" lay-verify="date" placeholder="开始时间" autocomplete="off" class="layui-input datePicker">
      </div>
    </div>
    -
     <div class="layui-inline">  
      <div class="layui-input-inline">
        <input type="text" name="publishTimeEnd" id="publishTimeEnd" lay-verify="date" placeholder="结束时间" autocomplete="off" class="layui-input datePicker">
      </div>
    </div>
       状态： 
     <div class="layui-input-inline">
      <select name="nstate" id="nstate" >
        <option value="">请选择状态</option> 
        <option value="0">起草</option>
        <option value="1">发表</option>
      </select>
    </div>
     
    <button id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
</div>
 
<table class="layui-hide" id="Orgnews" lay-filter="Orgnews"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        <button class="layui-btn layui-btn-sm" lay-event="previewHtml">预览</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit"  title="编辑" >编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="publish"  title="发表" >发表</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"  title="删除" >删除</a>
</script>


<script th:src="@{/alllayui/layui/layui.js}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['jquery','table','form','laydate'], function(){
        var table = layui.table; 
        var form = layui.form
        ,layer = layui.layer
        ,$ = layui.$ ;
   	    var laydate = layui.laydate;
        
        table.render({
            elem: '#Orgnews'
            ,url:'/orgnews/list'
            ,method: 'post'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: 'Orgnews'
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
              ,{field: 'id', title:'ID',  align:'center' }
              ,{field: 'newsType', title:'新闻类型',  align:'center' , templet: function(d){return dict(d,'newsType');} }
              ,{field: 'ntitle', title:'新闻标题',  align:'center' }
              ,{field: 'ncontent', title:'新闻内容',  align:'center' }
              ,{field: 'publishTime', title:'发表时间',  align:'center' }
              ,{field: 'userId', title:'发表用户ID',  align:'center' }
              ,{field: 'userName', title:'发表人',  align:'center' }
              ,{field: 'newsPath', title:'静态路径',  align:'center' }
              ,{field: 'nstate', title:'状态',  align:'center', templet: function(d){return dict(d,'nstate');} }
              ,{field: 'allowComment', title:'是否开启评论',  align:'center', templet: function(d){return dict(d,'allowComment');} } 
              ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]
            ]
            ,page: true
            ,request: {
                pageName: 'currentPageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.totalRecords, //解析数据长度
                    "data": res.recordList //解析数据列表
                };
            }

        });

        
        //同时绑定多个
        lay('.datePicker').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });

        var $ = layui.$, active = {
            reload: function(){
                var publishTimeStart  = $('#publishTimeStart');
                var publishTimeEnd = $('#publishTimeEnd');
                var nstate$_ = $('#nstate');
                var queryWhere = {};
                if(publishTimeStart.val()!=""  && publishTimeEnd.val()!="" ){
                	queryWhere['publishTimeStart'] = publishTimeStart.val();
                	queryWhere['publishTimeEnd'] = publishTimeEnd.val();
                } 
                if(nstate$_.val()!=""){
                	queryWhere['nstate'] = nstate$_.val();
                } 
                //执行重载
                table.reload('Orgnews', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , method: 'POST'
                    ,where: queryWhere
                    ,done: function(res, curr, count){
                    	this.where = {};
                    }
                }, 'data');
            }
        };

 

        $('.searchTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(Orgnews)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                //自定义头工具栏右侧图标 - 提示
                case 'add':
                    //iframe层-父子操作
                {
                   var addIndex = layer.open({
                        type: 2,
                        area: ['700px', '450px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/orgnews/add'
                    });
                    layer.full(addIndex);
                }
                    break;
                    
                    
                case 'previewHtml':
                	{
                	 var ary = checkStatus.data;
                	 if(ary.length!=1) break;
                	 if(ary[0].newsPath){
                		 window.open(ary[0].newsPath,'_blank','',false )
                	 }
                	}
                	break;
                    
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(Orgnews)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    $.post("/orgnews/delete",{"id":data.id},function (data) {
                    	layer.msg(data.message);
                    },"json");
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                //iframe层-父子操作
               var updateIndex = layer.open({
                    type: 2,
                    area: ['700px', '450px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/orgnews/update?id=' + data.id
                });
               layer.full(updateIndex);
            } else if(obj.event === 'publish'){
            	layer.confirm('现在发表吗？', function(index){
                    
                    $.post("/orgnews/publish",{"id":data.id},function (data) {
                        layer.msg(data.message);
                    },"json");
                    layer.close(index);
                });
            }
        });

    });


</script>

</body>
</html>